import json

from aiogram import Bot
from fluentogram import TranslatorRunner
from redis import asyncio as aioredis

from dc_shop.config import Settings
from dc_shop.core.models.enums.currency import Currency
from dc_shop.core.utils.order import calculate_total_summ, generate_product_lines
from dc_shop.core.utils.redis import get_redis_value, set_redis_value
from dc_shop.database.services.discount import DiscountService
from dc_shop.database.services.payment_method import PaymentMethodService
from dc_shop.database.services.product import ProductService
from dc_shop.database.services.promocode import PromocodeService
from dc_shop.database.services.shop import ShopService
from dc_shop.database.services.user import UserService
from dc_shop.tg_bot.keyboards.inline import confirm_cart_kb
from dc_shop.tg_bot.services.locale import Localizator
from dc_shop.tg_bot.services.tg_helpers import delete_msg_by_id, send_tg_message
from dc_shop.worker.exception_handler import exception_handler


@exception_handler
async def send_cart_confirmation_msg(ctx, user_tg_id: int, promocode_id: int | None = None) -> None:
    bot: Bot = ctx["bot"]
    localizator: Localizator = ctx["localizator"]
    config: Settings = ctx["config"]
    redis: aioredis = ctx["redis"]
    user_service: UserService = ctx["user_service"]
    product_service: ProductService = ctx["product_service"]
    payment_method_service: PaymentMethodService = ctx["payment_method_service"]
    discount_service: DiscountService = ctx["discount_service"]
    promocode_service: PromocodeService = ctx["promocode_service"]
    shop_service: ShopService = ctx["shop_service"]

    user = await user_service.get_user_by_telegram_id(user_tg_id)
    shop = await shop_service.get_shop_by_delivery_zone_id(user.delivery_zone_id)

    locale: TranslatorRunner = localizator.get_by_locale(user.lang_code)

    message = await get_redis_value(redis, f"{user_tg_id}_notifications")
    if message:
        await delete_msg_by_id(bot, message["message_id"], message["chat_id"])

    cart = await user_service.get_user_cart(user_tg_id)

    txt = locale.get("cart-review-msg")

    # Получаем метод оплат для батт
    payment_method = await payment_method_service.get_payment_method_by_currency(Currency.thb)

    promocode_amount = None
    if promocode_id:
        promocode = await promocode_service.get_promocode_by_id(promocode_id)
        promocode_amount = promocode.amount

    cart_total = await calculate_total_summ(
        items=cart.products,
        shop_id=shop.id,
        payment_method=payment_method,
        product_service=product_service,
        discount_service=discount_service,
        promocode_value=promocode_amount,
    )

    txt += await generate_product_lines(
        items=cart.products,
        shop_id=shop.id,
        payment_method=payment_method,
        product_service=product_service,
        discount_service=discount_service,
        promocode_value=promocode_amount,
    )

    txt += locale.get(
        "cart-total-summ",
        cart_total=cart_total,
        currency_symbol=payment_method.currency_symbol.value,
    )

    msg = await send_tg_message(
        bot,
        user_tg_id,
        txt,
        reply_markup=confirm_cart_kb(locale, config.tg_bot.webapp_url, promocode_id),
    )
    value = json.dumps(
        {
            "message_id": msg.message_id,
            "chat_id": msg.chat.id,
        }
    )
    await set_redis_value(redis, "notification_messages", value)
